package com.datastructure.test.implementofdictionarytree;

import java.util.ArrayList;
import java.util.LinkedList;

public class ImplementOfDictionaryTree {

    public static void main(String[] args) {
        ImplementOfDictionaryTree implementOfDictionaryTree = new ImplementOfDictionaryTree();
        String[][] operators = {{"1","qwer"},{"1","qwe"},{"3","qwer"},
                {"4","q"},{"2","qwer"},{"3","qwer"},{"4","q"}};
        String[] strings = implementOfDictionaryTree.trieU(operators);
        System.out.println(strings);
    }
    public LinkedList<String> data = new LinkedList<>();
    public LinkedList<String> result = new LinkedList<>();
    public String[] trieU (String[][] operators) {
        // write code here
        String command;
        String content;
        for (String[] operator : operators) {
            command = operator[0];
            content = operator[1];
            switch (command) {
                case "1":
                    insert(content);
                    break;
                case "2":
                    delete(content);
                    break;
                case "3":
                    result.add(search(content));
                    break;
                case "4":
                    result.add(String.valueOf(prefixNumber(content)));
                    break;
                default:
                    break;
            }
        }
        String[] res = new String[result.size()];
        for (int i = 0; i < result.size(); i++) {
            res[i]= result.get(i);
        }
        return res;
    }

    public void insert(String word){
        data.add(word);
    }

    public void delete(String word){
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).equals(word)){
                data.remove(i);
                break;
            }
        }
    }

    public String search(String word){
        for (String datum : data) {
            if (datum.equals(word)) {
                return "YES";
            }
        }
        return "NO";
    }

    public int prefixNumber(String pre){
        int sum=0;
        for (String datum : data) {
            if (datum.startsWith(pre)) {
                sum++;
            }
        }
        return sum;
    }
}
